<template>
    <div class="manage">
        <el-dialog title="更新用户" :before-close="handleClose" :visible.sync="dialogVisible" width="40%">
            <!-- 用户的表单信息 -->
            <el-form ref="form" :inline="true" :rules="rules" :model="form" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input placeholder="请输入年龄" v-model="form.age"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker v-model="form.birth" type="date" value-format="yyyy-MM-DD" placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input placeholder="请输入地址" v-model="form.addr"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
        <div class="manage-header" style="padding-bottom: 20px;">
            <el-button type="primary" @click="handleAdd">
                +新增
            </el-button>
            <el-form :inline="true" :model="userForm">
                <el-form-item>
                    <el-input @input="onSubmitChang($event)" placeholder="请输入名称" v-model="userForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-table">
            <el-table stripe border heigth="90%" :data="tableData" style="width: 100%;">
                <el-table-column prop="name" label="姓名">
                </el-table-column>
                <el-table-column prop="sex" label="性别">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.sex == 1 ? '男' : '女' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="age" label="年龄">
                </el-table-column>
                <el-table-column prop="birth" label="出身日期">
                </el-table-column>
                <el-table-column prop="addr" label="地址">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pager">
                <el-pagination background layout="prev, pager, next" :total="total" @current-change="handlePage">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from '@/api'
export default {
    name: 'User',
    data() {
        return {
            dialogVisible: false,
            form: {
                name: '',
                age: '',
                sex: '',
                birth: '',
                addr: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名', },
                ],
                age: [
                    { required: true, message: '请输入年龄', },
                ],
                sex: [
                    { required: true, message: '请选择性别', },
                ],
                birth: [
                    { required: true, message: '请选择出生日期', },
                ],
                addr: [
                    { required: true, message: '请填写地址', },
                ],
            },
            tableData: [],
            /**
             * 0表示新增弹窗
             * 1表示编辑天窗
             */
            modalType: 0,
            total: 0,
            pageData: {
                page: 1,
                limit: 10,
            },
            userForm: {
                namg: ''
            }
        };
    },
    methods: {
        // 提交用户表单
        submit() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    /* 处理逻辑 */
                    if (this.modalType == 0) {
                        let result = await addUser(this.form)
                        result = result.data
                        if (result.code == 20000 || result.code == 200) {
                            this.getUserList()
                        }
                    } else {
                        let result = await editUser(this.form)
                        result = result.data
                        if (result.code == 20000 || result.code == 200) {
                            this.getUserList()
                        }
                    }
                }
                this.form = {}
                this.$refs.form.resetFields()
                this.dialogVisible = false;
            })
        },
        handleAdd() {
            this.modalType = 0
            this.dialogVisible = true
        },
        handleClose(done) {
            /* 弹窗关闭的时候 */
            this.form = {}
            this.$refs.form.resetFields()
            done()

        },
        cancel() {
            this.form = {}
            this.dialogVisible = false;
            this.$refs.form.resetFields()
        },
        /* 获取数据 */
        async getUserList() {
            let result = await getUser({ params: { ...this.userForm, ...this.pageData } })
            result = result.data
            if (result.code == 20000 || result.code == 200) {
                this.tableData = result.list
                this.total = result.count || 0
            }
        },
        /* 编辑 */
        handleEdit(row) {
            this.modalType = 1
            this.dialogVisible = true
            this.form = JSON.parse(JSON.stringify(row))
        },
        /* 删除 */
        handleDelete(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let result = await delUser({ id: row.id })
                result = result.data
                if (result.code == 20000 || result.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getUserList()
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /* 选择页码回调 */
        handlePage(val) {
            this.pageData.page = val
            this.getUserList()
        },
        /* 列表的查询 */
        onSubmit() {
            this.getUserList()
        },
        onSubmitChang(event) {
            setTimeout(() => {
                if (event.trim() == '') {
                    this.getUserList()
                }
            }, 1000)
        },
    },
    mounted() {
        this.getUserList()
    },
}
</script>

<style lang="less" scoped>
.manage {
    height: 90%;

    .el-table {
        height: 90%;
        overflow: auto;
    }

    .common-table {
        position: relative;

        .pager {
            position: absolute;
            right: 20px;
            bottom: -60px;
        }
    }

    .manage-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
}
</style>